package cn.wolfcode.p2p.base.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.RealAuthQueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AssertUtil;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {

	@Autowired
	private RealAuthMapper realAuthMapper;

	@Autowired
	private IUserInfoService userinfoService;

	@Override
	public RealAuth get(Long id) {
		return this.realAuthMapper.selectByPrimaryKey(id);
	}

	@Override
	public void audit(Long id, int state, String remark) {
		// 检查,实名认证对象处于待审核状态;
		RealAuth ra = this.realAuthMapper.selectByPrimaryKey(id);
		AssertUtil.isNotNull(ra, "参数异常");
		AssertUtil.isTrue(ra.getState() == RealAuth.STATE_NORMAL, "状态异常");
		// 设置审核相关属性
		ra.setAuditor(UserContext.getCurrent());
		ra.setAuditTime(new Date());
		ra.setRemark(remark);
		ra.setState(state);
		// 如果审核通过;
		UserInfo applier = this.userinfoService.get(ra.getApplier().getId());
		if (state == RealAuth.STATE_PASS) {
			// 1,用户添加状态吗;
			applier.addState(BitStateUtil.OP_REAL_AUTH);
			// 2,userinfo设置冗余数据;
			applier.setRealName(ra.getRealName());
			applier.setIdNumber(ra.getIdNumber());
		} else {
			// 如果审核拒绝;
			// 把usreinfo的realauthid删除;
			applier.setRealAuthId(null);
		}
		this.realAuthMapper.updateByPrimaryKey(ra);
		this.userinfoService.update(applier);
	}

	@Override
	public PageInfo<RealAuth> query(RealAuthQueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<RealAuth> list = this.realAuthMapper.queryData(qo);
		return new PageInfo<>(list);
	}

	@Override
	public void apply(RealAuth to) {
		UserInfo current = this.userinfoService.get(UserContext.getCurrent().getId());
		// 判断,用户没有通过实名认证,没有审核当中的;
		AssertUtil.isTrue(!current.getHasRealAuth(), "用户已经通过实名认证");
		AssertUtil.isTrue(current.getRealAuthId() == null, "用户已经有申请中");

		// 创建一个实名认证对象,设置相关值;
		RealAuth r = new RealAuth();
		r.setAddress(to.getAddress());
		r.setApplier(UserContext.getCurrent());
		r.setApplyTime(new Date());
		r.setBornDate(to.getBornDate());
		r.setIdNumber(to.getIdNumber());
		r.setImage1(to.getImage1());
		r.setImage2(to.getImage2());
		r.setRealName(to.getRealName());
		r.setSex(to.getSex());
		r.setState(RealAuth.STATE_NORMAL);

		// 保存实名认证对象;
		this.realAuthMapper.insert(r);
		// 把id添加到userinfo的realauth;
		current.setRealAuthId(r.getId());
		// 修改userinfo
		this.userinfoService.update(current);
	}

}
